package org.jivesoftware.smack;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.jivesoftware.smack.filter.AndFilter;
import org.jivesoftware.smack.filter.IQTypeFilter;
import org.jivesoftware.smack.filter.PacketExtensionFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.PacketIDFilter;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.IQ.Type;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.Privacy;
import org.jivesoftware.smack.packet.PrivacyItem;

public class PrivacyListManager
{
  private static Map<Connection, PrivacyListManager> instances = new Hashtable();
  private Connection connection;
  private final List<PrivacyListListener> listeners = new ArrayList();
  PacketFilter packetFilter;

  static
  {
    Connection.addConnectionCreationListener(new ConnectionCreationListener()
    {
      public void connectionCreated()
      {
        new PrivacyListManager(paramConnection, null);
      }
    });
  }

  private PrivacyListManager(Connection paramConnection)
  {
    PacketFilter[] arrayOfPacketFilter = new PacketFilter[2];
    arrayOfPacketFilter[0] = new IQTypeFilter(IQ.Type.SET);
    arrayOfPacketFilter[1] = new PacketExtensionFilter("query", "jabber:iq:privacy");
    this.packetFilter = new AndFilter(arrayOfPacketFilter);
    this.connection = paramConnection;
    init();
  }

  public static PrivacyListManager getInstanceFor(Connection paramConnection)
  {
    return ((PrivacyListManager)instances.get(paramConnection));
  }

  private List<PrivacyItem> getPrivacyListItems(String paramString)
    throws XMPPException
  {
    Privacy localPrivacy = new Privacy();
    localPrivacy.setPrivacyList(paramString, new ArrayList());
    return getRequest(localPrivacy).getPrivacyList(paramString);
  }

  private Privacy getPrivacyWithListNames()
    throws XMPPException
  {
    return getRequest(new Privacy());
  }

  private Privacy getRequest(Privacy paramPrivacy)
    throws XMPPException
  {
    paramPrivacy.setType(IQ.Type.GET);
    paramPrivacy.setFrom(getUser());
    PacketIDFilter localPacketIDFilter = new PacketIDFilter(paramPrivacy.getPacketID());
    PacketCollector localPacketCollector = this.connection.createPacketCollector(localPacketIDFilter);
    this.connection.sendPacket(paramPrivacy);
    Privacy localPrivacy = (Privacy)localPacketCollector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    localPacketCollector.cancel();
    if (localPrivacy == null)
      throw new XMPPException("No response from server.");
    if (localPrivacy.getError() != null)
      throw new XMPPException(localPrivacy.getError());
    return localPrivacy;
  }

  private String getUser()
  {
    return this.connection.getUser();
  }

  private void init()
  {
    instances.put(this.connection, this);
    this.connection.addConnectionListener(new ConnectionListener(this)
    {
      public void connectionClosed()
      {
        PrivacyListManager.access$1().remove(PrivacyListManager.access$2(this.this$0));
      }

      public void connectionClosedOnError()
      {
      }

      public void reconnectingIn()
      {
      }

      public void reconnectionFailed()
      {
      }

      public void reconnectionSuccessful()
      {
      }
    });
    this.connection.addPacketListener(new PacketListener(this)
    {
      // ERROR //
      public void processPacket()
      {
        // Byte code:
        //   0: aload_1
        //   1: ifnull +10 -> 11
        //   4: aload_1
        //   5: invokevirtual 29	org/jivesoftware/smack/packet/Packet:getError	()Lorg/jivesoftware/smack/packet/XMPPError;
        //   8: ifnull +4 -> 12
        //   11: return
        //   12: aload_1
        //   13: checkcast 31	org/jivesoftware/smack/packet/Privacy
        //   16: astore_2
        //   17: aload_0
        //   18: getfield 19	org/jivesoftware/smack/PrivacyListManager$3:this$0	Lorg/jivesoftware/smack/PrivacyListManager;
        //   21: invokestatic 35	org/jivesoftware/smack/PrivacyListManager:access$3	(Lorg/jivesoftware/smack/PrivacyListManager;)Ljava/util/List;
        //   24: astore_3
        //   25: aload_3
        //   26: monitorenter
        //   27: aload_0
        //   28: getfield 19	org/jivesoftware/smack/PrivacyListManager$3:this$0	Lorg/jivesoftware/smack/PrivacyListManager;
        //   31: invokestatic 35	org/jivesoftware/smack/PrivacyListManager:access$3	(Lorg/jivesoftware/smack/PrivacyListManager;)Ljava/util/List;
        //   34: invokeinterface 41 1 0
        //   39: astore 5
        //   41: aload 5
        //   43: invokeinterface 47 1 0
        //   48: ifne +56 -> 104
        //   51: aload_3
        //   52: monitorexit
        //   53: new 8	org/jivesoftware/smack/PrivacyListManager$3$1
        //   56: dup
        //   57: aload_0
        //   58: invokespecial 50	org/jivesoftware/smack/PrivacyListManager$3$1:<init>	(Lorg/jivesoftware/smack/PrivacyListManager$3;)V
        //   61: astore 11
        //   63: aload 11
        //   65: getstatic 56	org/jivesoftware/smack/packet/IQ$Type:RESULT	Lorg/jivesoftware/smack/packet/IQ$Type;
        //   68: invokevirtual 62	org/jivesoftware/smack/packet/IQ:setType	(Lorg/jivesoftware/smack/packet/IQ$Type;)V
        //   71: aload 11
        //   73: aload_1
        //   74: invokevirtual 66	org/jivesoftware/smack/packet/Packet:getFrom	()Ljava/lang/String;
        //   77: invokevirtual 70	org/jivesoftware/smack/packet/IQ:setFrom	(Ljava/lang/String;)V
        //   80: aload 11
        //   82: aload_1
        //   83: invokevirtual 73	org/jivesoftware/smack/packet/Packet:getPacketID	()Ljava/lang/String;
        //   86: invokevirtual 76	org/jivesoftware/smack/packet/IQ:setPacketID	(Ljava/lang/String;)V
        //   89: aload_0
        //   90: getfield 19	org/jivesoftware/smack/PrivacyListManager$3:this$0	Lorg/jivesoftware/smack/PrivacyListManager;
        //   93: invokestatic 80	org/jivesoftware/smack/PrivacyListManager:access$2	(Lorg/jivesoftware/smack/PrivacyListManager;)Lorg/jivesoftware/smack/Connection;
        //   96: aload 11
        //   98: invokevirtual 85	org/jivesoftware/smack/Connection:sendPacket	(Lorg/jivesoftware/smack/packet/Packet;)V
        //   101: goto -90 -> 11
        //   104: aload 5
        //   106: invokeinterface 89 1 0
        //   111: checkcast 91	org/jivesoftware/smack/PrivacyListListener
        //   114: astore 6
        //   116: aload_2
        //   117: invokevirtual 95	org/jivesoftware/smack/packet/Privacy:getItemLists	()Ljava/util/Map;
        //   120: invokeinterface 101 1 0
        //   125: invokeinterface 104 1 0
        //   130: astore 7
        //   132: aload 7
        //   134: invokeinterface 47 1 0
        //   139: ifeq -98 -> 41
        //   142: aload 7
        //   144: invokeinterface 89 1 0
        //   149: checkcast 106	java/util/Map$Entry
        //   152: astore 8
        //   154: aload 8
        //   156: invokeinterface 109 1 0
        //   161: checkcast 111	java/lang/String
        //   164: astore 9
        //   166: aload 8
        //   168: invokeinterface 114 1 0
        //   173: checkcast 37	java/util/List
        //   176: astore 10
        //   178: aload 10
        //   180: invokeinterface 117 1 0
        //   185: ifeq +22 -> 207
        //   188: aload 6
        //   190: aload 9
        //   192: invokeinterface 120 2 0
        //   197: goto -65 -> 132
        //   200: astore 4
        //   202: aload_3
        //   203: monitorexit
        //   204: aload 4
        //   206: athrow
        //   207: aload 6
        //   209: aload 9
        //   211: aload 10
        //   213: invokeinterface 124 3 0
        //   218: goto -86 -> 132
        //
        // Exception table:
        //   from	to	target	type
        //   27	53	200	finally
        //   104	204	200	finally
        //   207	218	200	finally
      }
    }
    , this.packetFilter);
  }

  private Packet setRequest(Privacy paramPrivacy)
    throws XMPPException
  {
    paramPrivacy.setType(IQ.Type.SET);
    paramPrivacy.setFrom(getUser());
    PacketIDFilter localPacketIDFilter = new PacketIDFilter(paramPrivacy.getPacketID());
    PacketCollector localPacketCollector = this.connection.createPacketCollector(localPacketIDFilter);
    this.connection.sendPacket(paramPrivacy);
    Packet localPacket = localPacketCollector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    localPacketCollector.cancel();
    if (localPacket == null)
      throw new XMPPException("No response from server.");
    if (localPacket.getError() != null)
      throw new XMPPException(localPacket.getError());
    return localPacket;
  }

  public void addListener(PrivacyListListener paramPrivacyListListener)
  {
    synchronized (this.listeners)
    {
      this.listeners.add(paramPrivacyListListener);
      return;
    }
  }

  public void createPrivacyList(String paramString, List<PrivacyItem> paramList)
    throws XMPPException
  {
    updatePrivacyList(paramString, paramList);
  }

  public void declineActiveList()
    throws XMPPException
  {
    Privacy localPrivacy = new Privacy();
    localPrivacy.setDeclineActiveList(true);
    setRequest(localPrivacy);
  }

  public void declineDefaultList()
    throws XMPPException
  {
    Privacy localPrivacy = new Privacy();
    localPrivacy.setDeclineDefaultList(true);
    setRequest(localPrivacy);
  }

  public void deletePrivacyList(String paramString)
    throws XMPPException
  {
    Privacy localPrivacy = new Privacy();
    localPrivacy.setPrivacyList(paramString, new ArrayList());
    setRequest(localPrivacy);
  }

  public PrivacyList getActiveList()
    throws XMPPException
  {
    boolean bool;
    Privacy localPrivacy = getPrivacyWithListNames();
    String str = localPrivacy.getActiveName();
    if ((localPrivacy.getActiveName() != null) && (localPrivacy.getDefaultName() != null) && (localPrivacy.getActiveName().equals(localPrivacy.getDefaultName())))
      bool = true;
    while (true)
    {
      return new PrivacyList(true, bool, str, getPrivacyListItems(str));
      bool = false;
    }
  }

  public PrivacyList getDefaultList()
    throws XMPPException
  {
    boolean bool;
    Privacy localPrivacy = getPrivacyWithListNames();
    String str = localPrivacy.getDefaultName();
    if ((localPrivacy.getActiveName() != null) && (localPrivacy.getDefaultName() != null) && (localPrivacy.getActiveName().equals(localPrivacy.getDefaultName())))
      bool = true;
    while (true)
    {
      return new PrivacyList(bool, true, str, getPrivacyListItems(str));
      bool = false;
    }
  }

  public PrivacyList getPrivacyList(String paramString)
    throws XMPPException
  {
    return new PrivacyList(false, false, paramString, getPrivacyListItems(paramString));
  }

  public PrivacyList[] getPrivacyLists()
    throws XMPPException
  {
    Privacy localPrivacy = getPrivacyWithListNames();
    Set localSet = localPrivacy.getPrivacyListNames();
    PrivacyList[] arrayOfPrivacyList = new PrivacyList[localSet.size()];
    int i = 0;
    Iterator localIterator = localSet.iterator();
    while (true)
    {
      if (!(localIterator.hasNext()))
        return arrayOfPrivacyList;
      String str = (String)localIterator.next();
      arrayOfPrivacyList[i] = new PrivacyList(str.equals(localPrivacy.getActiveName()), str.equals(localPrivacy.getDefaultName()), str, getPrivacyListItems(str));
      ++i;
    }
  }

  public void setActiveListName(String paramString)
    throws XMPPException
  {
    Privacy localPrivacy = new Privacy();
    localPrivacy.setActiveName(paramString);
    setRequest(localPrivacy);
  }

  public void setDefaultListName(String paramString)
    throws XMPPException
  {
    Privacy localPrivacy = new Privacy();
    localPrivacy.setDefaultName(paramString);
    setRequest(localPrivacy);
  }

  public void updatePrivacyList(String paramString, List<PrivacyItem> paramList)
    throws XMPPException
  {
    Privacy localPrivacy = new Privacy();
    localPrivacy.setPrivacyList(paramString, paramList);
    setRequest(localPrivacy);
  }
}